<script src="../router/index.js"></script>
<template>
  <div>
    <div class="a">
    <el-button  icon="el-icon-circle-plus-outline" type="success" @click="add">新增</el-button>
    </div>
    <!--弹出表单框 model框-->
    <el-dialog title="新增信息" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="网址名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="网址" :label-width="formLabelWidth">
          <el-input v-model="form.add" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="upok">确 定</el-button>
      </div>

    </el-dialog>

    <el-table
        :data="list"
        border
        style="width: 100%">
      <!--网址名称-->
      <!--prop="title"循环数组的一项-->
      <el-table-column
          align="center"
          label="网址名称"
          width="300">
        <template slot-scope="scope">{{scope.row.title?scope.row.title:'meiyou'}}</template>
      </el-table-column>
      <!--网址url-->
      <el-table-column
          label="网址"
          align="center"
          width="300">
        <template slot-scope="scope">{{scope.row.title?scope.row.title:'meiyou'}}</template>
      </el-table-column>
      <!--   操作 删除-->
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.id)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
  export default {
    name: "friendlink",
    data() {
      return {
        list: [],
        dialogFormVisible: false,
        form: {
          name: '',
          add: '',
        },
        formLabelWidth: '100px',
        // 本地定义token
        token: '$2a$10$lZrcfKnKCrDTsCvs1jEEi.Y9wpCnpqKWow2Pj6dCVnwhZXXuj/9Pi-15b4a5f1-b8ed-4fdb-a69b-534dbc58f07d',
      }
    },
    filters:{//filters过滤器 过滤list里面数据
      ftitle(e){
        // console.log(e)
        return e.split('*')[0]
      },
      ltitle(e){
        // console.log(e)
        return e.split('*')[1]
      },

    },
    methods: {
      getlist() {//获取数据
        this.axios.post(this.API.API1 + '/getAllNews', {state: 150}, {
          headers: {
            'Authorization': 'XIEGE ' + this.API.token,
            'Content-Type': 'application/json;charset=UTF-8'
          }
        }).then(res => {
          console.log(res.data)
          // 字符串处理split
          // res.data.forEach((item,index,arr)=>{
          //  res.data[index].title = item.title.split('*')
          // })
          // console.log(res.data)
          this.list = res.data
        })

      },

      add() {//点击新增
        this.dialogFormVisible = true
      },

      upok() {//确认新增 信息
        this.$confirm('是否修改信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post(this.API.API1 + '/addNews', {
            state: 150,
            title: this.form.name + "*" + this.form.add
          }, {
            headers: {
              'Authorization': 'XIEGE ' + this.API.token,
              'Content-Type': 'application/json;charset=UTF-8'
            }
          }).then(res => {
            if (res.data === "success") {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.dialogFormVisible = false
              this.getlist()
              this.form = {}
            }
            console.log(res)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },

      handleDelete(id) {//删除方法
        console.log(id)
        this.$confirm('是否修改信息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post(this.API.API1 + '/delNews', {
            state: 150,
            // title: this.form.name + "*" + this.form.add,
            id:id
          }, {
            headers: {
              'Authorization': 'XIEGE ' + this.API.token,
              'Content-Type': 'application/json;charset=UTF-8'
            }
          }).then(res => {
            console.log(res)
            if (res.data === "success") {
              this.$message({
                type: 'success',
                message: '修改成功!'
              })
              this.dialogFormVisible = false
              this.getlist()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })

      },
    },

    created() {
      this.getlist()
    }
  }
</script>

<style scoped>

</style>